<?php
	session_start();

	/*
	Info: Handles the voting system
	Author: Pritam Pebam
	Version: 1.0
	Contact: ptamzz@gmail.com; @ptamzz
	URL: http://12bubbles.com
	*/
	
	require_once('../inc/fnc.php');
	//require_once('../inc/conn.php');
	require_once('../inc/settings.php');
	
	// Initializing variables
	session_start();
	$uniqueid = mysql_prep($_POST['uniqueid']);
	$storyId = mysql_prep($_POST['storyid']);
	$vote = mysql_prep($_POST['vote']);

	$user = $_SESSION['user'];
	$userId = $_SESSION['userId'];
	if($userId != null){
		$type = 'vote';
		$return = 0;

		
		$query = "SELECT * FROM entrylinevote where entrylineid = '{$uniqueid}' and votebyuserid = '{$userId}' and delflag = 'N'";
		$result = mysql_query($query,$connection);
		while($row = mysql_fetch_array($result)) {
			if(($row['vote'] == -1 && $vote == 1) || ($row['vote'] == 1 && $vote == -1)) {
				mysql_query("update entrylinevote set delflag = 'Y' where entrylineid = '{$uniqueid}' and votebyuserid = '{$userId}' and delflag = 'N'");
				mysql_query("insert into entrylinevote (storyid,entrylineid,votebyuserid,vote,crtdbyid,delflag,crtdtime) values ('{$storyId}','{$row['entrylineid']}','{$userId}','{$vote}','{$row['crtdbyid']}','N',NOW())");
				$return = 1;
			} elseif(($row['vote'] == 1 && $vote == 1) || ($row['vote'] == -1 && $vote == -1)) {
				$return = 2;
			}
		}
		
		if($return == 0) {
			
			$query = "SELECT * FROM entryline where displayseq = '{$uniqueid}' and delflag = 'N'";
			
			$result = mysql_query($query);
			while($row = mysql_fetch_array($result)) {
				$query = "insert into entrylinevote (storyid,entrylineid,votebyuserid,vote,crtdbyid,delflag,crtdtime) values ('{$storyId}','{$uniqueid}','{$userId}','{$vote}','{$row['crtdbyid']}','N',NOW())";
				mysql_query($query);
				$storylineid = $row['storylineid'];
			}
		}

		if($return != 2){
			addtonotification($storyId,$storylineid,$uniqueid,$vote);
		}

		
		$query = "SELECT count(vote) as votecount FROM entrylinevote where entrylineid = '{$uniqueid}' and vote = '1' and delflag = 'N'";
		$result = mysql_query($query,$connection);
		while($row = mysql_fetch_array($result)) {
			if($row['votecount'] >= VOTE_COUNT) {
				$voteCount = $row['votecount'];
				$query = "SELECT * FROM entryline where displayseq = '{$uniqueid}' and delflag = 'N'";
				$result = mysql_query($query,$connection);
				while($row1 = mysql_fetch_array($result)) {
					$storylineid = getnewstorylineid($storyId);
					$line = mysql_prep($row1['entryline']);
					$query = "INSERT INTO storyline (storyid,storylineid,storyline,totalvote,crtdbyid,delflag,crtdtime) 
											VALUES ('{$storyId}','{$storylineid}','{$line}','{$voteCount}','{$row1['crtdbyid']}','N',NOW())";
					$checkResult = mysql_query($query,$connection) or die('sql error');
					
					//set the line that has been upgraded to a storyline as deleted from entryline table
					$query = "UPDATE `entrylinevote` SET delflag = 'Y' WHERE `entrylineid` = '{$uniqueid}'";
					$result = mysql_query($query,$connection);
				}
				addtonotification($storyId,$storylineid,$uniqueid,4);
				if($checkResult){ $return = 4; } else { echo 'false'; }
			}
		}
		echo $return;
	} else {
		unset($_SESSION['indicator']);
		unset($_SESSION['redirecturl']);
		$_SESSION['redirecturl'] = $_SERVER['HTTP_REFERER'];
		$_SESSION['indicator'] = '11';
		$return = 5;
		echo $return;
	}
?>